package com.edu.exam.entity.assessment.technical;

import com.edu.exam.enums.assessment.CheckRecommendationType;
import com.edu.exam.enums.assessment.RecommendationStatus;
import com.edu.exam.enums.assessment.RecommendedAction;
import com.edu.exam.enums.assessment.SeverityLevel;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.Builder.Default;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 查重推荐实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：check_recommendation
 *
 * 根据B-02-06试题查重子模块设计文档实现
 * 管理查重推荐结果，提供智能化的试题查重建议和处理意见。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "check_recommendation")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CheckRecommendation {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 查重推荐唯一标识符
     */
    @NotBlank(message = "查重推荐ID不能为空")
    @Size(max = 50, message = "查重推荐ID长度不能超过50个字符")
    @Column(name = "recommendation_id", nullable = false, length = 50, unique = true)
    private String recommendationId;

    /**
     * 查重记录ID（外键关联到question_check_records）
     */
    @NotBlank(message = "查重记录ID不能为空")
    @Size(max = 50, message = "查重记录ID长度不能超过50个字符")
    @Column(name = "record_id", nullable = false, length = 50)
    private String recordId;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 相似试题ID（被推荐的相似试题ID）
     */
    @Column(name = "similar_question_id")
    private Long similarQuestionId;

    /**
     * 推荐类型（DUPLICATE-重复试题，SIMILAR-相似试题，MODIFY-建议修改，REVIEW-建议人工审核）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "recommendation_type", nullable = false, length = 20)
    private CheckRecommendationType recommendationType;

    /**
     * 推荐标题
     */
    @NotBlank(message = "推荐标题不能为空")
    @Size(max = 200, message = "推荐标题长度不能超过200个字符")
    @Column(name = "recommendation_title", nullable = false, length = 200)
    private String recommendationTitle;

    /**
     * 推荐内容
     */
    @NotBlank(message = "推荐内容不能为空")
    @Size(max = 2000, message = "推荐内容长度不能超过2000个字符")
    @Column(name = "recommendation_content", nullable = false, columnDefinition = "TEXT")
    private String recommendationContent;

    /**
     * 推荐理由（详细的推荐原因说明）
     */
    @Size(max = 1000, message = "推荐理由长度不能超过1000个字符")
    @Column(name = "recommendation_reason", columnDefinition = "TEXT")
    private String recommendationReason;

    /**
     * 相似度得分
     */
    @Column(name = "similarity_score", precision = 5, scale = 4)
    private BigDecimal similarityScore;

    /**
     * 置信度
     */
    @Column(name = "confidence_level", precision = 5, scale = 4)
    private BigDecimal confidenceLevel;

    /**
     * 优先级（1-5，数字越大优先级越高）
     */
    @Column(name = "priority_level")
    private Integer priorityLevel;

    /**
     * 严重程度（LOW-低，MEDIUM-中，HIGH-高，CRITICAL-严重）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "severity_level", length = 20)
    private SeverityLevel severityLevel;

    /**
     * 推荐动作（DELETE-删除，MODIFY-修改，MERGE-合并，REVIEW-人工审核，IGNORE-忽略）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "recommended_action", length = 20)
    private RecommendedAction recommendedAction;

    /**
     * 处理状态（PENDING-待处理，APPROVED-已采纳，REJECTED-已拒绝，IGNORED-已忽略）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private RecommendationStatus status;

    /**
     * 处理结果
     */
    @Size(max = 500, message = "处理结果长度不能超过500个字符")
    @Column(name = "processing_result", length = 500)
    private String processingResult;

    /**
     * 处理意见
     */
    @Size(max = 1000, message = "处理意见长度不能超过1000个字符")
    @Column(name = "processing_comment", columnDefinition = "TEXT")
    private String processingComment;

    /**
     * 处理人员ID
     */
    @Size(max = 50, message = "处理人员ID长度不能超过50个字符")
    @Column(name = "processed_by", length = 50)
    private String processedBy;

    /**
     * 处理人员姓名
     */
    @Size(max = 100, message = "处理人员姓名长度不能超过100个字符")
    @Column(name = "processed_by_name", length = 100)
    private String processedByName;

    /**
     * 处理时间
     */
    @Column(name = "processing_time")
    private LocalDateTime processingTime;

    /**
     * 影响范围（JSON格式，描述推荐的影响范围）
     */
    @Column(name = "impact_scope", columnDefinition = "TEXT")
    private String impactScope;

    /**
     * 风险评估（JSON格式，评估处理此推荐的风险）
     */
    @Column(name = "risk_assessment", columnDefinition = "TEXT")
    private String riskAssessment;

    /**
     * 建议方案（JSON格式，提供具体的处理建议）
     */
    @Column(name = "suggested_solution", columnDefinition = "TEXT")
    private String suggestedSolution;

    /**
     * 相关证据（JSON格式，支持推荐结论的证据）
     */
    @Column(name = "evidence_data", columnDefinition = "TEXT")
    private String evidenceData;

    /**
     * 规则依据（JSON格式，产生推荐的规则依据）
     */
    @Column(name = "rule_basis", columnDefinition = "TEXT")
    private String ruleBasis;

    /**
     * 算法信息（JSON格式，产生推荐的算法信息）
     */
    @Column(name = "algorithm_info", columnDefinition = "TEXT")
    private String algorithmInfo;

    /**
     * 历史相似记录（JSON格式，历史上的相似记录）
     */
    @Column(name = "historical_similar_records", columnDefinition = "TEXT")
    private String historicalSimilarRecords;

    /**
     * 自动处理标记（是否可自动处理）
     */
    @Column(name = "auto_processable")
    private Boolean autoProcessable;

    /**
     * 自动处理规则（JSON格式，自动处理的具体规则）
     */
    @Column(name = "auto_process_rules", columnDefinition = "TEXT")
    private String autoProcessRules;

    /**
     * 过期时间
     */
    @Column(name = "expiry_time")
    private LocalDateTime expiryTime;

    /**
     * 提醒时间
     */
    @Column(name = "reminder_time")
    private LocalDateTime reminderTime;

    /**
     * 是否已发送提醒
     */
    @Column(name = "reminder_sent")
    private Boolean reminderSent;

    /**
     * 关联推荐ID列表（JSON数组，相关的其他推荐）
     */
    @Column(name = "related_recommendations", columnDefinition = "TEXT")
    private String relatedRecommendations;

    /**
     * 标签列表（JSON数组，推荐标签）
     */
    @Column(name = "recommendation_tags", columnDefinition = "TEXT")
    private String recommendationTags;

    /**
     * 扩展属性（JSON格式，其他扩展信息）
     */
    @Column(name = "extended_attributes", columnDefinition = "TEXT")
    private String extendedAttributes;

    /**
     * 创建者ID
     */
    @NotBlank(message = "创建者ID不能为空")
    @Size(max = 50, message = "创建者ID长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 版本号（乐观锁）
     */
    @Version
    @Column(name = "version")
    @Default
    private Integer version = 0;

    /**
     * 软删除标识
     */
    @Column(name = "is_deleted", nullable = false)
    @Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;
}